package TUILayer;
import ControllerLayer.*;
import java.util.Scanner;

public class LoanUI
{
    private ToolStockController toolStockController;
    private LoanController loanController;
    private String idAssistant;

    public LoanUI(String idAssistant)
    {
        this.idAssistant = idAssistant;
        toolStockController= ToolStockController.getInstance();
        loanController= new LoanController();
    }
    
    public void start()
    {
        loanMenu();
    }
    
    public String getIDAssistant(){
        return idAssistant;
    }
    
    private void loanMenu()
    {int id=0;
       boolean exit = false;
       while(!exit)
       {
          int choise = writeLoanMenu(); 
          if(choise == 1)
          { 
              prepareLoan();
          }
          else{
              if(choise == 2){
                   id=createLoan(); 
              }  
              else{
                  if(choise == 3){
                      printLoan(id);
                  }  
                    
                  else
                  {
                      if(choise==4){
                         returnLoan();
                        }
                  
                  
                  else{
                              exit = true;
                        }
                    }
                }
            
       }
    }
    }
    private int writeLoanMenu()
    {     
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Product Menu ***");
            System.out.println(" Logged as: " +getIDAssistant());
            System.out.println(" (1) What tools do you want to loan? :");
            System.out.println(" (2) Create loan");
            System.out.println(" (3) Print loan");
            System.out.println(" (4) Return loan");
            System.out.println(" (5) Return");
            System.out.print("\n Make your choice: ");
            
            int choise = keyboard.nextInt();
            
            return choise;
        }
        
     private int inputID()
    {
        boolean ok = false;
        int id=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Customer  ID :  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number");
             String input = keyboard.nextLine();
           }
        }
        return id;
    } 
    
    private int inputYear()
    {
        boolean ok = false;
        int nr=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Input year :  ");
           try{
             nr = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number");
             String input = keyboard.nextLine();
           }
        }
        return nr;
    } 
    
    private int inputMonth()
    {
        boolean ok = false;
        int nr=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Input month :  ");
           try{
             nr = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number");
             String input = keyboard.nextLine();
           }
        }
        return nr;
    } 
    
    private int inputDay()
    {
        boolean ok = false;
        int nr=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Input Day :  ");
           try{
             nr = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number");
             String input = keyboard.nextLine();
           }
        }
        return nr;
    } 
   
     
    private String  inputBarCode()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Input item code: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    private String confirmLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Confirm loan? (y/n): ");
        String address = keyboard.nextLine();
        return address;
    }
    
    private void prepareLoan()
    {
        loanController.resetToolLoanedList();
        boolean ok=false;
        while(!ok)
        {
            String barCode=inputBarCode();
            if(!barCode.equals(""))
            {
               try{
                               
                   loanController.prepareLoan( barCode);
                }
              
              catch(NullPointerException ex){
                    System.out.println("There are no available tools ");
                    
              }
            }
            
            else {
                System.out.println("Preparing loan finished. Press enter for the next step");
                ok=true;
                
            }
        }
           
        
        inputContinue();
    }
    
    private int createLoan()
    {   int id =inputID();
        String assistantID= getIDAssistant();
        int year=inputYear();
        int month=inputMonth();
        int day= inputDay();
        String s= confirmLoan();
        if(s.equals("y")||s.equals("Y")){
            id=loanController.createLoan(id, assistantID, year, month, day);
            System.out.println("Loan was succesfully created.");
            inputContinue();
        }
        else
        {
            System.out.println("Loan has not been created, please try again");
            inputContinue();
        }
        
        return id;
     }
    
    private void printLoan(int id)
    {
        System.out.println(loanController.printLoan(id));
        inputContinue();
    }
    
    private void returnLoan()
    {int id =inputID();
        int year=inputYear();
        int month=inputMonth();
        int day= inputDay();
        System.out.println(loanController.printInvoiceLoan(id,year,month,day));
        inputContinue();
    }
        
    private void inputContinue()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Press Enter to continue:  ");
         String code = keyboard.nextLine();
    }    
}